using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;
namespace DAL
{
    public class tbLoaiSPDAL
    {
        private SqlConnection cn = null;
        public tbLoaiSPDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbLoaiSP loaisp)
        {
            SqlCommand cm = new SqlCommand("sp_LoaiSP_insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenLoai", loaisp.TenLoai );
            cm.ExecuteNonQuery();
        }
        public void Update(tbLoaiSP loaisp)
        {
            SqlCommand cm = new SqlCommand("sp_LoaiSP_update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdLoaiSP", loaisp.IdLoaiSP );
            cm.Parameters.AddWithValue("@TenLoai", loaisp.TenLoai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_LoaiSP_delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdLoaiSP", Id);
            cm.ExecuteNonQuery();
        }
        public tbLoaiSPCollection Get(string Where, int PageIndex,
                                                 int PageSize, string OderBy,
                                                 string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_LoaiSP_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader ();
            tbLoaiSPCollection loaispcollection = new tbLoaiSPCollection();
            while (dr.Read())
            {
                tbLoaiSP tbloaisp = new tbLoaiSP();
                tbloaisp.IdLoaiSP = int.Parse(dr.GetValue(0).ToString());
                tbloaisp.TenLoai = dr.GetValue(1).ToString();
                loaispcollection.Add(tbloaisp);
            }
            dr.Close();
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return loaispcollection;
        }
    }
}
